package com.aplink.generic.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.widget.ArrayAdapter;

import com.aplink.generic.database.DatabaseHelper;
import com.aplink.generic.pojo.ContentData;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.controlspec.actionbar.SelectionSpec;
import com.aplink.generic.util.ResourcesHelper;
import com.aplink.generic.xmlparser.ContentDataParser;

public class ActionbarSelectionAdapter extends ArrayAdapter<String> {
    private final ContentData contentData;
    private final String mDisplayColumn;
    private final ArrayList<GenericObject> mGenericObjects;
    private final SelectionSpec mSelectionSpec;
    private final String mValueColumn;

    public ActionbarSelectionAdapter(final Context context,
            final SelectionSpec selectionSpec) {
        super(context, android.R.layout.simple_spinner_dropdown_item);
        this.mSelectionSpec = selectionSpec;
        final int xmlFileId = ResourcesHelper.getRawIdByName(selectionSpec
                .getContentData());
        this.contentData = ContentDataParser.getInstance().parseIntoObjProp(
                xmlFileId);
        this.mGenericObjects = DatabaseHelper.getInstance().getList(
                this.contentData, null);
        this.mDisplayColumn = this.contentData.getMainTable() + "."
                + this.mSelectionSpec.getDisplayProperty();
        this.mValueColumn = this.contentData.getMainTable() + "."
                + this.mSelectionSpec.getValueProperty();
    }

    @Override
    public int getCount() {
        return this.mGenericObjects.size();
    }

    @Override
    public String getItem(final int position) {
        return this.mGenericObjects.get(position).getValue(this.mDisplayColumn);
    }

    public String getValue(final int position) {
        return this.mGenericObjects.get(position).getValue(this.mValueColumn);
    }
}
